/**
 * @param {string} s
 * @return {boolean}
 */
const leftToRight = {
  "(": ")",
  "{": "}",
  "[": "]",

}

var isValid = function (s) {
  if (!s) {
    return true
  }
  //初始化一个栈
  const stack = []
  const len = s.length
  for (let i = 0; i < len; i++) {
    // 遇到左括号就入栈
    // 否则就将栈中的元素弹出
    const ch = s[i]
    if (ch === '(' || ch === '{' || ch === '[') {
      stack.push(leftToRight[ch])
    } else {
      if (!stack.length || stack.pop() !== ch) {
        return false
      }
    }
  }
  return !stack.length
}